using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SunshineFarmWord
{
    public class SoilDig : InteractiveBase
    {
        //挖坑的四个素材 顺序排放
        [Header("挖坑的四个素材顺序摆放")]
        [field: SerializeField]
        private List<Sprite> soilSpriteList;

        [Header("赋值的图片资源")]
        [field: SerializeField]
        private SpriteRenderer soilSprite;
        [field: SerializeField, Header("农作物节点")]
        private GameObject cropsBaseNode;

        //素材索引
        private int _SpriteIndex = 0;

        //挖多少次更新一次状态
        private int _digSoilNumber = 1;
        //多少秒恢复一次土坑
        private float _resetDigTime = 10f;
        //挖多少次
        private int _digSoilCount = 0;
        //是否可以进行挖坑
        private bool _isDigDoing = true;
        //总共需要挖坑的次数
        private int _digSoilTotalCount = 1;

        void Awake()
        {
            soilSprite.sprite = soilSpriteList[_SpriteIndex];
        }


        protected override void Start()
        {
            base.Start();
        }

        protected override void Update()
        {
            base.Update();
        }

        #region 交互方法
        public override void InteractiveStart(PlayerStateMachine stateMachine)
        {
            base.InteractiveStart(stateMachine);

            if (IsPlayerDig(stateMachine)) stateMachine.ChangeState(stateMachine.DigState);
        }
        #endregion


        #region public function
        //初始化传入数据
        public void InitDigData(int digSoilNumber, float resetDigTime)
        {
            _digSoilNumber = digSoilNumber;
            _resetDigTime = resetDigTime;
            _digSoilTotalCount = _digSoilNumber * (soilSpriteList.Count - 1);
        }
        //挖掘
        public void DigDoing()
        {
            _digSoilCount++;
            if (_digSoilCount % _digSoilNumber == 0)
            {
                UpdateDigRestoreOrHole(1);
            }
            if (_digSoilCount >= _digSoilTotalCount)
            {
                _isDigDoing = false;
                return;
            }
        }
        //是否可以进行挖掘
        public bool IsDigDoing()
        {
            return _isDigDoing;
        }
        //修改自己的tag为农作物
        public void ChangeTag()
        {
            gameObject.tag = PlayerStateReusableData.Collider2DTagEnum.DigOver.ToString();
        }
        //修改自己的tag为土坑
        public void ChangeTagToSoil()
        {
            gameObject.tag = PlayerStateReusableData.Collider2DTagEnum.Dig.ToString();
        }
        //挖掘完毕
        public void DigEnd()
        {
            _digSoilCount = 0;
            _isDigDoing = false;
            ChangeTag();
            //展示农作物节点
            cropsBaseNode?.SetActive(true);
        }
        #endregion

        #region Main function
        //向上/向下更新状态
        //传入-1表示恢复坑洞,传入1表示挖坑
        private void UpdateDigRestoreOrHole(int index)
        {
            _SpriteIndex += index;
            _SpriteIndex = Mathf.Clamp(_SpriteIndex, 0, soilSpriteList.Count - 1);
            soilSprite.sprite = soilSpriteList[_SpriteIndex];
        }

        //是否可以挖坑
        private bool IsPlayerDig(PlayerStateMachine stateMachine)
        {
            bool isDig = false;
            if (stateMachine.ReusableData.PlayerStatesData.PlayerDigData.digSoilNode == null) return isDig;
            if (stateMachine.ReusableData.PlayerStatesData.PlayerDigData.digSoilNode.TryGetComponent<SoilDig>(out SoilDig soilDig))
                isDig = soilDig.IsDigDoing();
            //Debug.Log("可以挖坑吗？");
            //Debug.Log(isDig);
            return isDig;
        }
        #endregion
    }
}
